package com.siwei.mes.tonghang;

import com.alibaba.fastjson.JSON;
import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.base.BaseParams;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.bus.SyncGroup;
import com.siwei.mes.entity.Project;
import com.siwei.mes.mapper.ProjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * @Author Administrator
 * @Date 2023/11/30 15:49
 * @Version 1.0
 * @Description 砼行工单接口
 */
@RestController
@Api(tags = "对外给砼行的接口")
@RequestMapping("tonghang/project")
public class ThProjectController {
    private static final Logger log = LoggerFactory.getLogger("tonghang");

    @Value("${tonghang-token}")
    private String TONGHANG_TOKEN;
    @Resource
    private ProjectMapper projectMapper;

    /**
     * 校验token
     *
     * @param request
     */
    public void checkToken(HttpServletRequest request) {
        String token = request.getHeader(BaseParams.Authorization);
        assertTrue(StringUtils.isBlank(token), "授权token为空");
        assertTrue(!token.equals(TONGHANG_TOKEN), "token不正确");
    }

    @AnonymousAccess
    @ApiOperation(value = "新增或删除工程")
    @PostMapping("/saveOrUpdate")
    public Json saveOrUpdate(@RequestBody @Validated(SyncGroup.class) Project entity, HttpServletRequest request) {
        log.info("砼行新增或修改工程,入参：{}", JSON.toJSONString(entity));
        this.checkToken(request);
        assertTrue(StringUtils.isBlank(entity.getThId()), "砼行工程id为空");
        /**根据syncId判断是否存在*/
        Project localInfo = projectMapper.selectByThId(entity.getThId());
        if (localInfo != null) {
            entity.setId(localInfo.getId());
            projectMapper.updateByPrimaryKeySelective(entity);
        } else {
            projectMapper.insertSelective(entity);
        }
        return Json.succ().data(entity);
    }

    @AnonymousAccess
    @ApiOperation(value = "删除工程")
    @PostMapping("/delete")
    public Json delete(@RequestBody Project entity, HttpServletRequest request) {
        this.checkToken(request);
        assertTrue(StringUtils.isBlank(entity.getThId()), "砼行工程id为空");
        log.info("砼行删除工程,入参：{}", entity.getThId());
        Project localInfo = projectMapper.selectByThId(entity.getThId());
        if (localInfo != null) {
            projectMapper.deleteByPrimaryKey(localInfo.getId());
        }
        return Json.succ().data(new ApiResult());
    }
}
